﻿using System;
using WSKnowledge.WSHelpers.DesignByContract;

namespace WSKnowledge.WSHelpers
{
	public class OutcomeStatus
	{
		public bool IsSuccess { get; protected set; }
		public string ErrorMessage { get; protected set; }
		public Exception CaughtException { get; protected set; }

		protected OutcomeStatus() { }
		protected OutcomeStatus(string errorMessage, Exception caughtException)
		{
			ErrorMessage = errorMessage;
			CaughtException = caughtException;
		}

		public static void SetToSuccess(out OutcomeStatus status)
		{
			status = new OutcomeStatus
			{
				IsSuccess = true,
				ErrorMessage = null,
				CaughtException = null
			};
		}

		public static void SetToFailure(out OutcomeStatus status, string errorMessage)
		{
			Check.Require(errorMessage.IsNotEmpty());

			status = new OutcomeStatus
			{
				IsSuccess = false,
				ErrorMessage = errorMessage,
				CaughtException = null
			};
		}

		public static void SetToFailure(out OutcomeStatus status, string errorMessage, Exception caughtException)
		{
			Check.Require(errorMessage.IsNotEmpty());
			Check.Require(caughtException != null);

			status = new OutcomeStatus
			{
				IsSuccess = false,
				ErrorMessage = errorMessage,
				CaughtException = caughtException
			};
		}

		public static void SetToFailure(out OutcomeStatus status, Exception caughtException)
		{
			Check.Require(caughtException != null);

			status = new OutcomeStatus
			{
				IsSuccess = false,
				ErrorMessage = caughtException.Message,
				CaughtException = caughtException
			};
		}

		public override string ToString()
		{
			return IsSuccess ?
				"Successful done" : String.Format("Failed to process request.\nErrorMessage: {0}\nCaughtException: {1}",
				ErrorMessage, CaughtException);
		}
	}
}
